//
//  HXLWeatherTableViewCell.m
//  On The Load
//
//  Created by 晓琳 on 15/11/16.
//  Copyright © 2015年 @@叶一闽. All rights reserved.
//

#import "HXLWeatherTableViewCell.h"
#import "BaseCollectionView.h"
#import "HXLWeatherCollectionViewCell.h"
#import "WeekModel.h"
#import "DrawLine.h"

@interface HXLWeatherTableViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) BaseCollectionView *weatherCV;
@property (nonatomic, strong) UIImageView *imageViewN;
@property (nonatomic, strong) UIImageView *backImageView1;
@property (nonatomic, assign) BOOL did;

@end

@implementation HXLWeatherTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubviews];
        [self createImageViewN];
        self.did = NO;
    }
    return self;
}


- (void) createSubviews{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumInteritemSpacing = 75/6*SCALEW;
    flowLayout.itemSize = CGSizeMake(50, 400);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.weatherCV = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 10, WIDTH, 400) collectionViewLayout:flowLayout];
    self.weatherCV.delegate = self;
    self.weatherCV.dataSource  = self;
    self.weatherCV.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.weatherCV];
    self.weatherCV.showsHorizontalScrollIndicator = NO;
    
    
    [self.weatherCV registerClass:[HXLWeatherCollectionViewCell class] forCellWithReuseIdentifier:@"weatherReuse"];
}


- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSLog(@"week = %ld",self.weekArray.count);
    return self.weekArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HXLWeatherCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"weatherReuse" forIndexPath:indexPath];
    
    cell.weekModel = [self.weekArray objectAtIndex:indexPath.row];
    return cell;
}
- (void) setWeekArray:(NSMutableArray *)weekArray {
    NSLog(@"www%@", weekArray);
    _weekArray = weekArray;
//    if (_did == NO) {
//    
//        [self drawLine];
//
//        self.did = YES;
//    }
    [self drawLine];

    
    
        [self.weatherCV reloadData];

}

-(void) createImageViewN {
    self.imageViewN = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 400, self.weatherCV.frame.size.height)];
    [self.weatherCV addSubview:self.imageViewN];
}

//画高温低温线
- (void)drawLine {
    
#warning self.weekArray.count-1 self.weekArray.count-1无限大,UI将被卡死,所以加判断解决
    if (self.weekArray.count > 0) {
        
        for (int i = 0; i < self.weekArray.count - 1; i++) {
            WeekModel *model1 = [self.weekArray objectAtIndex:i];
            WeekModel *model2 = [self.weekArray objectAtIndex:i+1];
            
            CGFloat high1 = [model1.temp_high floatValue];
            CGFloat high2 = [model2.temp_high floatValue];
            
            [DrawLine drawCenter:self.imageViewN withBeginX:30 * SCALEW + (60 * SCALEW + 10) * i withBeginY:170 * SCALEH - high1 * 2.25 withEndX:30 * SCALEW + (60 * SCALEW + 10) *
             (i + 1) withEndY:170 * SCALEH - high2 * 2.25 withLineWidth:2 * SCALEW withColorR:1 G:1 B:1 Alpha:1];
            
        }
        
        for (int i = 0; i < self.weekArray.count-1; i++) {
            WeekModel *model1 = [self.weekArray objectAtIndex:i];
            WeekModel *model2 = [self.weekArray objectAtIndex:i+1];
            CGFloat high1 = [model1.temp_low floatValue];
            CGFloat high2 = [model2.temp_low floatValue];
            
            
            [DrawLine drawCenter:self.imageViewN withBeginX:30 * SCALEW + (60 * SCALEW + 10) * i withBeginY:240 * SCALEH - high1 * 2.25 withEndX:30 * SCALEW + (60 * SCALEW + 10) *
             (i + 1) withEndY:240 * SCALEH - high2 * 2.25 withLineWidth:2 * SCALEW withColorR:1 G:1 B:1 Alpha:1];
            
        }
        
        
    }
    
    
}


@end
